using System;
using System.Linq;
using System.Timers;
using System.Windows.Threading;
using WPFCommon.Interface;
using WPFCommon.ViewModels.Base;

namespace ALC.Core.ViewModels.Chart
{
    public class LineChartViewModel : ViewModelBase
    {

        private IDataProvider _dataProvider;
        private int _dataLength;
        
        public double[] Y { get; private set; }

        public double NewValue { get; set; }
        public int[] X { get; private set; }

        public string Title { get; set; }

        public LineChartViewModel(double interval, int dataLength, IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _dataLength = dataLength;
            X = Enumerable.Range(1, dataLength).ToArray();
            Y = Enumerable.Repeat(0.0, dataLength).ToArray();

            _dataProvider.NextDataReady += OnTimerTick;
        }
        
        /// <summary>
        /// Get new value and update Data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTimerTick(double newValue)
        {
            NewValue = newValue;
            Y[_dataLength - 1] = NewValue;
            Array.Copy(Y, 1, Y, 0, _dataLength-1);
            OnPropertyChanged(nameof(Y));
        }
    }
}